package com.sojson.controller.po.pay;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.entity.po.TransferRecord;
import com.sojson.enums.ETransferRecordStatus;
import com.sojson.enums.core.EPayType;
import com.sojson.result.Result;
import com.sojson.service.TransferRecordService;
import com.sojson.service.pay.PayService;
import com.sojson.util.ArithUtil;
import com.sojson.util.StringUtil;

import io.swagger.annotations.Api;

/**
 * 支付控制器
 * 
 * @author liu
 * @date 2021-03-24
 */
@Api(tags = "支付管理")
@RestController
@RequestMapping(value = "/pay")
public class PayController {

    @Autowired
    private PayService payService;
    @Autowired
    private TransferRecordService transferRecordService;

    /**
     * 提现
     * 
     * @return
     * @throws Exception 
     */
    // @PostMapping("/withdrawDeposit")
    @PreAuthorize("@ss.hasPermi('/pay/withdrawDeposit')")
    public Object withdrawDeposit(@RequestBody TransferRecord transferRecord) throws Exception {
        Integer payType = transferRecord.getPayType();
        if (StringUtil.isBlankObject(payType)) {
            return Result.fail("付款类型不能为空");
        }
        if (payType == EPayType.OFFLINE.getCode()) {
            if (StringUtil.isBlankObject(transferRecord.getBank())) {
                return Result.fail("银行不能为空");
            }
            if (StringUtil.isBlankObject(transferRecord.getNoBank())) {
                return Result.fail("银行卡号不能为空");
            }
            if (StringUtil.isBlankObject(transferRecord.getNameUserBank())) {
                return Result.fail("持卡人不能为空");
            }
        }
        if (StringUtil.isBlankObject(transferRecord.getType())) {
            return Result.fail("提现类型不能为空");
        }
        BigDecimal total = transferRecord.getTotal();
        if (StringUtil.isBlankObject(total)) {
            return Result.fail("提现金额不能为空");
        }
        int intValue = total.intValue();
        if (intValue < 30) {
            return Result.fail("提现金额不能小于0.3");
        }
        if (intValue > 500000) {
            return Result.fail("提现金额不能大于5000");
        }
        transferRecord.setTotal(ArithUtil.intPennyToBigDollar(intValue));

        return Result.success(payService.withdrawDeposit(transferRecord));
    }

    /**
     * 提现申请
     * 
     * @return
     * @throws Exception 
     */
    @PostMapping("/withdrawDepositApplyFor")
    @PreAuthorize("@ss.hasPermi('/pay/withdrawDeposit')")
    public Object withdrawDepositApplyFor(@RequestBody TransferRecord transferRecord) throws Exception {
        Integer payType = transferRecord.getPayType();
        if (StringUtil.isBlankObject(payType)) {
            return Result.fail("付款类型不能为空");
        }
        if (payType == EPayType.OFFLINE.getCode()) {
            if (StringUtil.isBlankObject(transferRecord.getBank())) {
                return Result.fail("银行不能为空");
            }
            if (StringUtil.isBlankObject(transferRecord.getNoBank())) {
                return Result.fail("银行卡号不能为空");
            }
            if (StringUtil.isBlankObject(transferRecord.getNameUserBank())) {
                return Result.fail("持卡人不能为空");
            }
        }
        if (StringUtil.isBlankObject(transferRecord.getType())) {
            return Result.fail("提现类型不能为空");
        }
        BigDecimal total = transferRecord.getTotal();
        if (StringUtil.isBlankObject(total)) {
            return Result.fail("提现金额不能为空");
        }
        int intValue = total.intValue();
        if (intValue < 30) {
            return Result.fail("提现金额不能小于0.3");
        }
        if (intValue > 500000) {
            return Result.fail("提现金额不能大于5000");
        }
        transferRecord.setTotal(ArithUtil.intPennyToBigDollar(intValue));

        return Result.success(payService.withdrawDepositApplyFor(transferRecord));
    }

    /**
     * 提现审核
     * 
     * @return
     * @throws Exception 
     */
    @PutMapping("/withdrawDepositAudit")
    @PreAuthorize("@ss.hasPermi('/pay/withdrawDepositAudit')")
    public Object withdrawDepositAudit(@RequestBody TransferRecord transferRecord) throws Exception {
        String orderNo = transferRecord.getOrderNo();
        if (StringUtil.isBlankObject(orderNo)) {
            return Result.fail("订单编号不能为空！");
        }
        Integer status = transferRecord.getStatus();
        if (StringUtil.isBlankObject(status)) {
            return Result.fail("审核状态不能为空！");
        }
        TransferRecord tr = new TransferRecord();
        tr.setOrderNo(orderNo);
        tr.setStatus(status);
        tr.setMessage(transferRecord.getMessage());
        return Result.success(payService.withdrawDepositAudit(tr));
    }

    /**
     * 关闭提现订单
     * 
     * @return
     * @throws Exception 
     */
    @PutMapping("/close")
    @PreAuthorize("@ss.hasPermi('/pay/close')")
    public Object close(@RequestBody TransferRecord transferRecord) throws Exception {
        TransferRecord tr = new TransferRecord();
        tr.setOrderNo(transferRecord.getOrderNo());
        tr.setStatus(ETransferRecordStatus.SECCUSS.getCode());
        tr.setMessage(transferRecord.getMessage());
        return Result.success(transferRecordService.updatePartialByOrderNo(tr));
    }

    /**
     * 重新提现
     * 
     * @return
     * @throws Exception 
     */
    @PutMapping("/anewWithdrawDeposit/{orderNo}")
    @PreAuthorize("@ss.hasPermi('/pay/anewWithdrawDeposit/{orderNo}')")
    public Object anewWithdrawDeposit(@PathVariable String orderNo) throws Exception {
        if (StringUtil.isBlank(orderNo)) {
            return Result.fail("订单号不能为空！");
        }

        return Result.success(payService.anewWithdrawDeposit(orderNo));
    }

}